#pragma once

#include "stdafx.h"
#include "vec3.h"
#include <glm/gtc/type_ptr.hpp>
#include "VBO.h"

enum FrustumSide
{
	RIGHT	= 0,		// The RIGHT side of the frustum
	LEFT	= 1,		// The LEFT	 side of the frustum
	BOTTOM	= 2,		// The BOTTOM side of the frustum
	TOP		= 3,		// The TOP side of the frustum
	BACK	= 4,		// The BACK	side of the frustum
	FRONT	= 5			// The FRONT side of the frustum
}; 

class LuaScript;

class Frustum
{
	LuaScript *lua;
public:
	GLfloat angle, farDist, nearDist, heightNear, heightFar, widthNear, widthFar;

	vec3 farC,nearC;
	vec3 fPos, fDir, fUp;
	vec3 up, right;

	float mRot;

	vec3 ftl,ftr,fbl,fbr;
	vec3 ntl,ntr,nbl,nbr;
	vec3 farV,nearV,leftV,rightV,topV,btmV;
	
	enum { FTL, FBR, FBL, FTR, NTL, NTR, NBL, NBR };
	enum { FARV, BTMV, LEFTV, RIGHTV, NEARV, TOPV };
	vec3 FrustumPlanes[8];
	vec3 FrustumNormals[6];

	float fovy, aspectratio;

	VBO vbo; GLuint id1, id2;

	Frustum(void);
	~Frustum(void);
	// Initialise the OpenGL Frustum
	bool Init(void);
	// Update the frustum
	void Update(void);
	// Draw the frustum
	void Draw(void);
	// Perform containment check for a position with respect to the Frustum
	bool PointInFrustum(vec3 position);
	// If bCheckAll is true, will return true only if all points are within frustum
	// Else will return true when just one point is in the frustum
	bool PointsInFrustum(vec3 *points, int ArrSize, bool bCheckAll);

	bool IntersectsSphere(vec3 pos, float radius);
	bool IntersectsCube(vec3 pos, float radius);
	bool IntersectsBox(vec3 TopLeft, vec3 BottomRight);

	bool ContainsSphere(vec3 pos, float radius);
	bool ContainsCube(vec3 pos, float size);
	bool ContainsBox(vec3 TopLeft, vec3 BottomRight);

//	bool PointInFrustum( float x, float y, float z );
//	bool SphereInFrustum( float x, float y, float z, float radius );
//	bool CubeInFrustum( float x, float y, float z, float size );

	bool bSetFrustum;
};